package com.mission.game.creategame;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.mission.game.R;
import com.mission.game.components.CreateGPSHelper;
import com.mission.game.utils.GPSTask;
import com.mission.game.utils.Task;

public class CreateGPSTask extends CreateAbstractTask {

	private TextView _curLocationView;
	private static final String LAT_LON_FORMAT = "Current target is %s : %s";
	private static final int START_MAP = 8;
	private int _lat = 0, _lon = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		_curLocationView = (TextView) findViewById(R.id.gps_lat_and_lon);
		_lat = ((GPSTask) _task).getLatitude();
		_lon = ((GPSTask) _task).getLongitude();
		setLocationView();
	}

	private void setLocationView() {
		_curLocationView.setText(String.format(LAT_LON_FORMAT,
				Double.toString(_lat / 1E6),
				Double.toString(_lon / 1E6)));
	}

	public void onSetTargetClicked(View v) {
		Intent i = new Intent(getApplicationContext(), CreateGPSHelper.class);
		i.putExtra("latitude", _lat).putExtra("longitude", _lon);
		startActivityForResult(i, START_MAP);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == START_MAP) {
			Bundle extras = data.getExtras();
			if (extras != null) {
				if (extras.containsKey("latitude")) {
					_lat = extras.getInt("latitude");
				} else {
					_lat = 0;
				}
				if (extras.containsKey("longitude")) {
					_lon = extras.getInt("longitude");
				} else {
					_lon = 0;
				}
			}
			setLocationView();
		}
	}


	@Override
	protected int getScoreInput() {
		return R.id.gps_score_input;
	}

	@Override
	protected Task newSpecificTask() {
		return new GPSTask();
	}

	@Override
	protected int getDescriptionInput() {
		return R.id.gps_description_input;
	}

	@Override
	protected int getNameInput() {
		return R.id.gpstask_name_input;
	}

	@Override
	protected int getContentView() {
		return R.layout.create_gps_task;
	}

	@Override
	protected void specificBackToMissionClicked(View v) {
		((GPSTask) _task).setLatitude(_lat);
		((GPSTask) _task).setLongitude(_lon);
	}

}
